package org.example.day20240909.dao;

import org.example.day20240909.model.Department;
import org.example.day20240909.model.Employee;
import org.example.day20240909.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDao {
    String STATE_ON = "有效";
    String DELETE_ON = "删除";

    public Department findEmployeeDepartment(String inputDepartment) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String employeeSql = "select * from department where department_id = ?";
            ps = connection.prepareStatement(employeeSql);
            ps.setString(1, inputDepartment);
            rs = ps.executeQuery();
            while (rs.next()) {
                Department departmentReturn = new Department();
                departmentReturn.setDepartment_id(rs.getString("department_id"));
                departmentReturn.setDepartment_name(rs.getString("department_name"));
                departmentReturn.setDepartment_quota(rs.getDouble("department_quota"));
                departmentReturn.setEstablishment_date(rs.getString("establishment_date"));
                departmentReturn.setDepartment_head_name(rs.getString("department_head_name"));
                departmentReturn.setSuperior_department_name(rs.getString("superior_department_name"));
                departmentReturn.setEmployee_num(rs.getDouble("employee_num"));
                departmentReturn.setState(rs.getString("state"));
                return departmentReturn;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    public Department findEmployeeDepartmentId(String departmentName) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String employeeSql = "select * from department where department_name = ?";
            ps = connection.prepareStatement(employeeSql);
            ps.setString(1, departmentName);
            rs = ps.executeQuery();
            while (rs.next()) {
                Department departmentReturn = new Department();
                departmentReturn.setDepartment_id(rs.getString("department_id"));
                departmentReturn.setDepartment_name(rs.getString("department_name"));
                departmentReturn.setDepartment_quota(rs.getDouble("department_quota"));
                departmentReturn.setEstablishment_date(rs.getString("establishment_date"));
                departmentReturn.setDepartment_head_name(rs.getString("department_head_name"));
                departmentReturn.setSuperior_department_name(rs.getString("superior_department_name"));
                departmentReturn.setEmployee_num(rs.getDouble("employee_num"));
                return departmentReturn;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    public void addDepartment(Department department) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        try {
            connection.setAutoCommit(false);
            String sqlInsert = "insert into department " +
                    "(department_id" +
                    ",department_name" +
                    ",department_quota" +
                    ",establishment_date" +
                    ",department_head_name" +
                    ",superior_department_name" +
                    ",employee_num" +
                    ",state)" +
                    " values(?,?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sqlInsert);
            ps.setString(1, department.getDepartment_id());
            ps.setString(2, department.getDepartment_name());
            ps.setDouble(3, department.getDepartment_quota());
            ps.setString(4, department.getEstablishment_date());
            ps.setString(5, department.getDepartment_head_name());
            ps.setString(6, department.getDepartment_name());
            ps.setDouble(7, 1);
            ps.setString(8, STATE_ON);
            ps.executeUpdate();

            String sqlUpdate = "update employee set department_name = ? where department_head_name  = ?";
            ps = connection.prepareStatement(sqlUpdate);
            ps.setString(1, department.getDepartment_name());
            ps.setString(2, department.getDepartment_head_name());
            ps.executeUpdate();
            System.out.println("新增部门成功");
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            try {
                ps.close();
                connection.commit();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void updateDepartment(Department department) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        try {
            String sqlUpdate = "update department set " +
                    "department_name = ?" +
                    ",department_quota = ?" +
                    ",establishment_date = ?" +
                    ",department_head_name = ?" +
                    ",superior_department_name = ? " +
                    "where department_id = ?";
            ps = connection.prepareStatement(sqlUpdate);
            ps.setString(1, department.getDepartment_name());
            ps.setDouble(2, department.getDepartment_quota());
            ps.setString(3, department.getEstablishment_date());
            ps.setString(4, department.getDepartment_head_name());
            ps.setString(5, department.getDepartment_name());
            ps.setString(6, department.getDepartment_id());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void updateSameNameDepartment(Department department) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        try {
            String sqlUpdate = "update department set " +
                    " department_quota = ?" +
                    ",establishment_date = ?" +
                    ",department_head_name = ?" +
                    ",superior_department_name = ? " +
                    "where department_id = ?";
            ps = connection.prepareStatement(sqlUpdate);
            ps.setDouble(1, department.getDepartment_quota());
            ps.setString(2, department.getEstablishment_date());
            ps.setString(3, department.getDepartment_head_name());
            ps.setString(4, department.getDepartment_name());
            ps.setString(5, department.getDepartment_id());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void deleteDepartment(String inputId) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        try {
            String sqlUpdate = "update department set " +
                    " state = ?" +
                    "where department_id = ?";
            ps = connection.prepareStatement(sqlUpdate);
            ps.setString(1, DELETE_ON);
            ps.setString(2, inputId);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public List<Employee> displayDepartmentEmployee(String departmentId) {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String employeeSql = "select employee.employee_id" +
                    ", employee.employee_name" +
                    ", employee.gender" +
                    ", employee.birthday" +
                    ", employee.hire_date" +
                    ", employee.department_name" +
                    ", employee.state" +
                    " from employee  " +
                    "left join department" +
                    " on employee.department_name = department.department_name" +
                    " where department_id = ?";
            ps = connection.prepareStatement(employeeSql);
            ps.setString(1, departmentId);
            rs = ps.executeQuery();
            List<Employee> employeeList = new ArrayList<>();
            while (rs.next()) {
                Employee employeeReturn = new Employee();
                employeeReturn.setEmployee_id(rs.getString("employee_id"));
                employeeReturn.setEmployee_name(rs.getString("employee_name"));
                employeeReturn.setGender(rs.getString("gender"));
                employeeReturn.setBirthday(rs.getString("birthday"));
                employeeReturn.setHire_date(rs.getString("hire_date"));
                employeeReturn.setDepartment_name(rs.getString("department_name"));
                employeeReturn.setState(rs.getString("state"));
                employeeList.add(employeeReturn);
            }
            return employeeList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public List<Department> findAllDepartment() {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String employeeSql = "select * from department";
            ps = connection.prepareStatement(employeeSql);
            rs = ps.executeQuery();
            List<Department> departmentList = new ArrayList<>();
            while (rs.next()) {
                Department departmentReturn = new Department();
                departmentReturn.setDepartment_id(rs.getString("department_id"));
                departmentReturn.setDepartment_name(rs.getString("department_name"));
                departmentReturn.setDepartment_quota(rs.getDouble("department_quota"));
                departmentReturn.setEstablishment_date(rs.getString("establishment_date"));
                departmentReturn.setDepartment_head_name(rs.getString("department_head_name"));
                departmentReturn.setSuperior_department_name(rs.getString("superior_department_name"));
                departmentReturn.setEmployee_num(rs.getDouble("employee_num"));
                departmentReturn.setState(rs.getString("state"));
                departmentList.add(departmentReturn);
            }
            return departmentList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
